# send_mail.py
import yagmail
import os

def send_email(file_path: str):
    try:
        email_user = os.getenv("EMAIL_USER")
        email_pass = os.getenv("EMAIL_PASS")
        email_to = os.getenv("EMAIL_TO")
        email_host = os.getenv("EMAIL_HOST", "smtp.qq.com")  # 默认使用 QQ 邮箱

        if not (email_user and email_pass and email_to):
            print("⚠️ 未设置邮件环境变量 (EMAIL_USER/EMAIL_PASS/EMAIL_TO)，跳过发送邮件")
            return

        if not os.path.exists(file_path):
            print(f"⚠️ 附件文件不存在：{file_path}")
            return

        yag = yagmail.SMTP(user=email_user, password=email_pass, host=email_host)
        yag.send(
            to=email_to,
            subject="选股结果",
            contents=f"您好，今日的选股结果见附件：{os.path.basename(file_path)}",
            attachments=[file_path]
        )
        print(f"✅ 邮件已发送到 {email_to}，附件：{file_path}")

    except Exception as e:
        print(f"❌ 邮件发送失败: {e}")
